package com.jrj.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.jrj.domain.Area;
import com.jrj.domain.Page;
import com.jrj.service.AreaDaoService;
import com.opensymphony.xwork2.ActionSupport;

/**
 *@author zhiming.li
 *@version createTime：2011-12-20 上午11:53:00
 */
public class AreaAction extends ActionSupport{
		
	@Autowired
	private AreaDaoService areaDaoService;
	
	private List<Area> list;
	
	private String tree;
	
	private String id;
	
	private Page page =new Page();
	
	
	
	@Override
	public String execute() throws Exception {
		list  = areaDaoService.getAll();
		StringBuffer str =new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			Integer parentId = list.get(i).getParentId();
			Integer areaId = list.get(i).getAreaId();
			String name = list.get(i).getAreaName();
			if(parentId==0 || parentId==null){
				str.append("d.add("+areaId+",0,'"+name+"','My example tree');");
			}else{
				str.append("d.add("+areaId+","+parentId+",'"+name+"','My example tree');");
			}
		}
		tree = str.toString();
		System.out.println(tree);
		return "success";
	}

	
	public String getPage(){
		page.setPageSize(20);
		Integer count = areaDaoService.getCount();
		if(count<=0){
			return "error";
		}
		Integer pageCount;
		if(count%page.getPageSize()==0){
			pageCount =count/page.getPageSize();
		}else{
			pageCount =count/page.getPageSize()+1;
		}
		if(page.getNowPage()<=0){
			page.setNowPage(0);
		}
		if(page.getNowPage()>page.getPageCount()){
			page.setNowPage(pageCount);
		}
		
		System.out.println(page.getNowPage()*page.getPageSize()+"---"+page.getPageSize());
		list = areaDaoService.getP(page.getNowPage()*page.getPageSize(), page.getPageSize());
		return "page";
	}
	
	
	public String getChild() throws Exception {
		if(id==null){
			id="0";
		}
		list  = areaDaoService.getChildId(id);
		return "getChild";
	}
	
	
	public List<Area> getList() {
		return list;
	}

	public void setList(List<Area> list) {
		this.list = list;
	}

	public String getTree() {
		return tree;
	}

	public void setTree(String tree) {
		this.tree = tree;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public void setPage(Page page) {
		this.page = page;
	}
	
	
	
}
